package com.example.treeHole.utils;

public class TrieTree {
    private TrieNode root;

    public TrieTree() {
        root = new TrieNode();
    }
    public void addWord(String word) {
        TrieNode node = root;
        for (char c : word.toCharArray()) {
            if (!node.children.containsKey(c)) {
                node.children.put(c, new TrieNode(c));
            }
            node = node.children.get(c);
        }
        node.isEnd = true;
    }
    public String filter(String text) {
        StringBuilder sb = new StringBuilder();
        StringBuilder result = new StringBuilder();
        TrieNode node = root;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (node.children.containsKey(c)) {
                node = node.children.get(c);
                if (node.isEnd) {
                    // 敏感词结束，可以替换或跳过
                    //如果检测到了则返回给用户
                    result.append("1");
                    // 跳过敏感词剩余部分
                    while (i + 1 < text.length() && node.children.containsKey(text.charAt(i + 1))) {
                        i++;
                        node = node.children.get(text.charAt(i));
                    }
                    // 回到Trie树的根节点
                    node = root;
                } else {
                    sb.append(c);
                }
            } else {
                // 字符不在Trie树中，直接添加到结果中
                sb.append(c);
            }
        }
        if (result.length() > 0){
            return "请等待管理员审批";
        }
        return sb.toString();
    }

    // 省略其他方法，例如removeWord等

}
